<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Web Parts list</title>
    <link rel="Stylesheet" href="../PageActionEditor.css" type="text/css" />
    <style type="text/css">
    div.Contents {
        width: 446px; 
        height: 300px; 
    }
    </style>
    <script type="text/javascript">
        var hiddenField;
        var statusChanged = false;

        function Init() {
            var hiddenFieldObject = new Object();

            document.body.onkeypress = function() {
                if (window.event.keyCode == 27) {
                    if (statusChanged) {
                        if (window.confirm("You have not saved your changes. if you close this dialog box now, you will lose your chianges.\n\nPress OK to close this dialog box, or Cancel to leave it open."))
                            self.close();
                        else
                            return;
                    }
                    else
                        self.close();
                }
            }
            
            if (typeof (dialogArguments) == "undefined") {
                document.getElementById("OKButton").disabled = true;
                return;
            }
        
            var webPartsArray = dialogArguments[1];
            if (webPartsArray.length == 0) {
                document.getElementById("OKButton").disabled = true;
                return;
            }

            hiddenField = dialogArguments[0];
            
            if (hiddenField.value != "")
                eval("hiddenFieldObject = " + hiddenField.value + ";");

            var table = document.getElementById("WebPartsList");
            for (var i = 0; i < webPartsArray.length; i++) {
                var row = table.insertRow(-1);
                var obj = hiddenFieldObject[webPartsArray[i][0]];
                var cell;

                cell = row.insertCell(-1);
                if (obj != undefined) {
                    cell.innerHTML = "<input type=\"checkbox\" onclick=\"this.parentNode.parentNode.children[2].firstChild.disabled = !this.checked; statusChanged = true;\" value=\"" + webPartsArray[i][0] + "\" checked />";

                    cell = row.insertCell(-1);
                    cell.innerHTML = webPartsArray[i][1];

                    cell = row.insertCell(-1);
                    cell.innerHTML = "<select onclick=\"statusChanged = true;\"><option value=\"1\"" + (obj == "1" ? " selected" : "") + ">Show this Web Part</option><option value=\"0\"" + (obj == "0" ? " selected" : "") + ">Hide this Web Part</option></select>";                
                }
                else {
                    cell.innerHTML = "<input type=\"checkbox\" onclick=\"this.parentNode.parentNode.children[2].firstChild.disabled = !this.checked; statusChanged = true;\" value=\"" + webPartsArray[i][0] + "\" />";

                    cell = row.insertCell(-1);
                    cell.innerHTML = webPartsArray[i][1];

                    cell = row.insertCell(-1);
                    cell.innerHTML = "<select onclick=\"statusChanged = true;\" disabled><option value=\"1\">Show this Web Part</option><option value=\"0\">Hide this Web Part</option></select>";
                }
            }
        }

        function UpdateHiddenField() {
            var hiddenValue = "";
            if (!document.getElementById("OKButton").disabled && hiddenField != null) {
                var table = document.getElementById("WebPartsList");
                for (var i = 0; i < table.rows.length; i++) {
                    var checkBox = table.rows[i].cells[0].firstChild;
                    var select = table.rows[i].cells[2].firstChild;

                    if (!checkBox.checked)
                        continue;

                    hiddenValue += checkBox.value + " : " + select.options[select.selectedIndex].value + ", ";
                }
                if (hiddenValue != "")
                    hiddenField.value = "{" + hiddenValue.substr(0, hiddenValue.length - 2) + "}";
                else
                    hiddenField.value = "";
            }
            self.close();
        }

        function CloseDialog() {
            if (statusChanged) {
                if (window.confirm("You have not saved your changes. if you close this dialog box now, you will lose your chianges.\n\nPress OK to close this dialog box, or Cancel to leave it open."))
                    self.close();
                else
                    return;
            }
            else
                self.close();
        }
    </script>
</head>
<body onload="Init()">
    <div class="Contents">
        <div class="ContentArea">
            <table id="WebPartsList">
                <tr>
                    <th>
                        <p>&nbsp;</p>
                    </th>
                    <th>
                        <p>
                            Web Part</p>
                    </th>
                    <th>
                        <p>
                            Actions</p>
                    </th>
                </tr>
            </table>
        </div>        
    </div>
    <div style="text-align: right; padding: 0px; padding-right: 3px; padding-top: 8px;">
        <input type="button" class="UserButton" value="OK" id="OKButton" onclick="UpdateHiddenField();" />&nbsp;<input type="button" class="UserButton" value="Cancel" onclick="CloseDialog()" />
    </div>
</body>
</html>
